function ajax({ type = 'get', data = null, url = '' }) {

    let token = sessionStorage.getItem('userToken')

    return new Promise((resolve, reject) => {
        $.ajax({
            type,
            url,
            data,
            beforeSend(xhr) {
                if (token) {
                    xhr.setRequestHeader("Authorization", token)
                }
            },
            dataType: "json",
            success: function(response) {
                resolve(response)
            }
        });
    })
}


let postUser = (data) => ajax({
    url: '/api/user/register',
    data,
    type: "post"
})

let userLogin = (data) => ajax({
    url: '/api/user/login',
    data,
    type: 'post'
})

let getToken = (data) => ajax({
    url: '/api/user/getToken',
    data
})

let addUserInfo = (data) => ajax({
    url: '/api/user/addInfo',
    data,
    type: 'post'
})

let sendArticle = (data) => ajax({
    url: '/api/article/saveArticle',
    data,
    type: 'post'
})

let updatePassword = (data) => ajax({
    url: '/api/user/updatePassword',
    data,
    type: 'post'
})

let getArticleList = (data) => ajax({
    url: '/api/article/articleList',
    data
})

let delArticle = (data) => ajax({
    url: '/api/article/delArticle',
    data,
    type: 'post'
})

let sendRatings = (data) => ajax({
    url: '/api/article/ratings',
    data,
    type: 'post'
})

let saveLikeNum = (data) => ajax({
    url: '/api/article/saveLike',
    data,
    type: 'post'
})